import Button from "@/component/Button";
import { Paths } from "@/Paths";
import { useState } from "react";
import { useNavigate } from "react-router";
import Show from "../../component/Show";
import { ApiHttp } from "../../network/ApiHttp";
import { GlobalStore } from "../../store/GlobalStore";
import { log_error } from "./log_error";
import { Space } from "./Space";
import { Text } from "@/gimd";

export function MemberRow() {
  const [{ is_me, username }, dispatch] = GlobalStore.useStore();
  const [submitting, setSubmitting] = useState(false);
  const nav = useNavigate();
  const onClickAddBlog = () => nav(Paths.All.Edtior);
  const onClickLogout = () => {
    if (submitting) return;
    setSubmitting(true);
    ApiHttp.post(`user/logout`, void 0, void 0)
      .catch(ApiHttp.ignoreAbort)
      .catch(log_error)
      .finally(() => setSubmitting(false))
      .finally(() => dispatch({ type: 'logout', payload: null }));
  };
  return (
    <Space>
      <Show yes={is_me}>
        <Button size='s' name="add_blog" onClick={onClickAddBlog}
          disabled={submitting}>写点</Button>
      </Show>
      <Button size='s'>
        你好，{username}
      </Button>
      <Button size='s' name="logout" onClick={onClickLogout}
        disabled={submitting}>登出
      </Button>
    </Space>
  );
}
